<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/11 11:52
 * Remark:
 */

namespace App\Models\Approval;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;

class OldDingTalkApprovalTemplateModel extends BaseModel
{
    protected $table = TableConsts::CY_DING_TALK_APPROVAL_TEMPLATE_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_DING_TALK_APPROVAL_TEMPLATE_TABLE_PRIMARY_KEY;

    protected $fillable = [
        "ding_talk_approval_template_id",
        "ding_talk__approval_template_code",
        "ding_talk__approval_template_name",
        "status",
        "created_at",
        "updated_at",
    ];


    public function isExistByCondition($conditionData){
        return $this
            ->withoutGlobalScopes()
            ->firstOrnew($conditionData)->exists;
    }

    public function getOldDingTalkApprovalTempSelect($dingTalkApprovalTempSelectType){

        $query =  $this->select(
            [
                'ding_talk_approval_template_id',
                'ding_talk_approval_template_id as key',
                'ding_talk_approval_template_name'
            ]
        );
        if($dingTalkApprovalTempSelectType){
            $query = $query->where('ding_talk_approval_template_type',$dingTalkApprovalTempSelectType);
        }
        return $query->where('status',BaseConsts::STATUS_NORMAL)
            ->get();
    }

    /**
     * 修改审批模板
     * @param $talkApprovalTemp
     * @return mixed
     */
    public function editOldDingTalkApprovalTemp($talkApprovalTemp){
        return $this
            ->where('ding_talk_approval_template_id',$talkApprovalTemp['ding_talk_approval_template_id'])
            ->update($talkApprovalTemp);
    }

    /**
     * 新增钉钉审批模板
     * @param $talkApprovalTemp
     * @return mixed
     */
    public function addOldDingTalkApprovalTemp($talkApprovalTemp){
        return $this->insertGetId($talkApprovalTemp);
    }


    public function getOldDingTalkApprovalTempList($query,$pageSize): mixed
    {
        return $query->where('status',BaseConsts::STATUS_NORMAL)
            ->latest()
            ->Paginate($pageSize);
    }

    public function getOldDingTalkApprovalTempInfo($talkApprovalTempId,$select=["*"]){
        return $this
            ->select($select)
            ->where('ding_talk_approval_template_id',$talkApprovalTempId)
            ->first();
    }
}